package com.company.woye.quantitycheck;

import android.os.Bundle;
import android.text.InputType;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.company.woye.R;
import com.company.woye.base.BaseActivity;
import com.company.woye.quantitycheck.adapter.QuantityCheckAdapter;
import com.company.woye.utils.ScannerManager;

import butterknife.BindView;
import butterknife.OnClick;

public class QuantityCheck2Activity extends BaseActivity {

    @BindView(R.id.iv_inventory_back)
    ImageView ivInventoryBack;
    @BindView(R.id.tv_inventory_back)
    TextView tvInventoryBack;
    @BindView(R.id.tv_inventory_search)
    TextView tvInventorySearch;
    @BindView(R.id.tv_code)
    EditText editBarcode;
    @BindView(R.id.tv_old_code)
    TextView tvOldCode;
    @BindView(R.id.tv_all_piece)
    TextView tvAllPiece;
    @BindView(R.id.tv_order)
    TextView tvOrder;
    @BindView(R.id.tv_bad)
    TextView tvBad;
    @BindView(R.id.tv_daoci)
    TextView tvDaoci;
    @BindView(R.id.tv_num)
    TextView tvNum;
    @BindView(R.id.tv_ganghao)
    TextView tvGanghao;
    @BindView(R.id.tv_piece)
    TextView tvPiece;
    @BindView(R.id.tv_mldj)
    TextView tvMldj;
    @BindView(R.id.tv_sb)
    TextView tvSb;
    @BindView(R.id.tv_all_bad)
    TextView tvAllBad;
    @BindView(R.id.lv_part)
    ListView lvPart;
    private ScannerManager scannerManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public int setLayoutId() {
        return R.layout.activity_quantity_check2;
    }

    @Override
    public void initView() {
        editBarcode.setInputType(InputType.TYPE_NULL);
        editBarcode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                editBarcode.setInputType(InputType.TYPE_CLASS_TEXT);
                InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
                imm.showSoftInput(editBarcode, InputMethodManager.SHOW_FORCED);
            }
        });
        editBarcode.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (event != null) {
                    if ((event.getAction() == KeyEvent.ACTION_UP) && (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
//                        search(SearchType);
                        return true;
                    }
                }
                return false;
            }
        });
        editBarcode.requestFocus();
        editBarcode.requestFocusFromTouch();
        //拦截扫描事件
        scannerManager = new ScannerManager(new ScannerManager.OnScanListener() {
            @Override
            public void onResult(String code, String deviceName) {
                editBarcode.setText(code);
            }
        }, editBarcode.getContext());

        lvPart.setAdapter(new QuantityCheckAdapter(this, null));

    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if (scannerManager.dispatchKeyEvent(event)) {
            return true;
        }
        return super.dispatchKeyEvent(event);
    }

    @OnClick(R.id.iv_inventory_back)
    public void onViewClicked() {
        finish();
    }
}
